/*
 * Copyright (c) gitee.com/jiandong001.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import { ReactNativeHost } from './ReactNativeHost';
import hilog from '@ohos.hilog';
import Ability from '@ohos.application.Ability'
import Window from '@ohos.window'
import { ReactActivityDelegate } from './ReactAbilityDelegate'
//

const PERMISSIONS: Array<string> = [
//  'ohos.permission.RUN_ANY_CODE',
  'ohos.permission.READ_MEDIA',
  'ohos.permission.WRITE_MEDIA',
  'ohos.permission.CAPTURE_SCREEN']


export abstract class ReactAbility extends Ability {
  static readonly TAG: string = "ReactAbility";
  private mDelegate: ReactActivityDelegate

  onCreate(want, launchParam) {
    this.context.requestPermissionsFromUser(PERMISSIONS,  (err, data)=>{
      hilog.info(0x0000, ReactAbility.TAG, "====>requestdata====>" + JSON.stringify(data));
      hilog.info(0x0000, ReactAbility.TAG, "====>requesterrcode====>" + JSON.stringify(err.code));
    })

    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'onCreate' +
      " bundleCodeDir: " + this.context?.bundleCodeDir +
      " cacheDir: " + this.context?.cacheDir +
      " tempDir: " + this.context?.tempDir +
      " filesDir: " + this.context?.filesDir);

    //todo: should not use single instance mode
    ReactNativeHost.getInstance().getReactInstanceManagerBuilder()
      .setAbilityContext(this.context)
      .setJSMainModulePath(this.getJSMainModuleName()) //index
      .setUseDeveloperSupport(ReactNativeHost.getInstance().getUseDeveloperSupport())
        //.setJSBundleFile("")
      .setBundleResourceName(ReactNativeHost.getBundleResourceName()) //index.android.bundle -> index.harmony.bundle ??
      .setJsEngineAsArk(true)
      .setJavaScriptExecutorFactory(ReactNativeHost.getJavaScriptExecutorFactory())
      .setUIImplementationProvider(ReactNativeHost.getUIImplementationProvider())
      .setJSIModulesPackage(ReactNativeHost.getJSIModulePackage())
      .build()

    this.mDelegate = this.createReactActivityDelegate()
    this.mDelegate.onCreate(want, launchParam);
  }

  onWindowStageCreate(windowStage: Window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'Ability onWindowStageCreate');
    this.mDelegate?.onWindowStageCreate(windowStage);
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'onWindowStageDestroy');
    this.mDelegate?.onWindowStageDestroy();
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'onForeground');
    this.mDelegate?.onForeground();
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'onBackground');
    this.mDelegate?.onBackground();
  }

  onDestroy() {
    hilog.info(0x0000, ReactAbility.TAG, '%{public}s', 'onDestroy');
    this.mDelegate?.onDestroy();
  }

  // must be implemented by subclass
  protected abstract getMainComponentName(): string
  protected abstract getJSMainModuleName(): string


  protected getHarmonyPageName(): string {
    return 'pages/index';
  }

  protected createReactActivityDelegate(): ReactActivityDelegate {
    return new ReactActivityDelegate(this.getMainComponentName(), this.getHarmonyPageName());
  }
}
